<?php
function equivUsertype($usertype){

	if($usertype == ADMIN)
	return 'Admin';
	else if($usertype == STAFF)
	return 'Staff';
	else if($usertype == CLIENT)
	return "Client";
	else if($usertype == MANAGER)
	return 'Manager';
}
$uname = time();
?>
<script type="text/javascript">

	function isClient(obj){

		if(obj.value != 'C'){
			$("#username").val("");
			$("#password").val("");
			$("#username").removeAttr("readonly");
			$("#password").removeAttr("readonly")
			$("#locationContainer").css("display","none");
		}else{
			$("#locationContainer").css("display","");
			$("#username").val("<?php echo $uname;?>");
			$("#password").val("skybest");
			$("#username").attr("readonly","true");
			$("#password").attr("readonly","true");
		}
		
	}




</script>
<?php if($allowed){ ?>
<br>
<div class="submenu">
	<a href='<?php echo ADMINBASEURL;?>users/view/client'>Clients</a>| <a
		href='<?php echo ADMINBASEURL;?>users/view/staff'>Staffs</a>| <a
		href='<?php echo ADMINBASEURL;?>users/view/admin'>Admin</a> <font
		color="blue"> if client user is not admin filter list by warehouse </font>
</div>
<?php } ?>



<!-- End Header -->

<!-- Container -->
<div id="container">
	<div class="shell">

		<!-- Small Nav -->
		<div class="small-nav">
			<a href="#">Dashboard</a> <span>&gt;</span> Current Users
		</div>
		<?php
		$label = 'users';
		include 'includes/message.php';
		?>
		<br />
		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>

			<!-- Content -->
			<div id="content">

				<!-- Box -->
				<div class="box" style="width: 1000px">
				<?php
				if($action == ''){
					?>
					<!-- Box Head -->
					<div class="box-head">
						<h2 class="left">Current Users</h2>
						<div class="right">
							<!--  
							<label>search Users</label>
							<input type="text" class="field small-field" />
							<input type="submit" class="button" value="search" />
						-->
						</div>
					</div>
					<!-- End Box Head -->

					<!-- Table -->
					<div class="table" style="width: 100%">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>

								<th>Date Created:</th>
								<th>Full Name</th>
								<th>Username</th>
								<th>Email</th>
								<th>Usertype</th>
								<th width="110" class="ac">Action</th>
							</tr>

							<?php
							if ($totalRecords > 0)
							{
								foreach ($query->result() as $user)
								{
									echo "
											<tr>
												<td>".$user->createdon."</td>
												<td>".$user->firstname." ".$user->lastname."</td>
												<td>".$user->username."</td>
												<td>".$user->email."</td>
												<td>".equivUsertype($user->usertype)."</td>
												<td><a href='".ADMINBASEURL."users/edit/".$user->id."'  class='ico edit'>edit</a> | <a href='".ADMINBASEURL."users/delete/".$user->id."'   class='ico del' onclick='return confirm(\"Are you sure to delete this User?\")'>delete</a></td>
											</tr>";


								}
							}
							?>




						</table>

						<?php
						include 'includes/pagination.php';
						?>

					</div>
					<!-- Table -->



				</div>
				<!-- End Box -->

				<?php
				}else{
					?>


				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>
						<?php
						if($action == 'edit')
						echo 'Edit ';
						else
						echo 'Add New ';
						?>
							User
						</h2>
					</div>
					<!-- End Box Head -->

					<form action="<?php echo ADMINBASEURL."users/save/"; ?>"
						method="post" onsubmit="return MyForm.validate(this);">

						<div class="form">

							<table width="100%">
							<?php if($action =='edit' && isset($user)){ ?>
								<tr>
									<td colspan="2" style="text-align: left">
										<p>
											<label>Client Track Number:<span> <?php echo (!isset($user))?'':$user->referencenumber; ?>
											</span> </label>
										</p>
									</td>
									<td style="text-align: right">
										<p>
											<label><span> <?php
											if($user->usertype == 'C'){
												?> <a
													href="<?php echo "/skybest/index.php/transaction/create/".$user->id; ?>">Add
														Transaction for this User</a> </span> </label>
														<?php
											}
											?>
										</p>
									</td>
								</tr>
								</tr>
								<?php }?>
								<tr>

									<td>
										<p>
											<label>Usertype<span>(Required Field)</span> </label>
											<?php if($action =='edit' && isset($user)){
												echo '<input type="hidden" name="id" value="'.$user->id.'">';
												echo '<input type="hidden" name="usertype" value="'.$user->usertype.'">';
												echo '<label><span>'.equivUsertype($user->usertype).'</span></label>';
											}else{
												?>
											<select class="field size3" name='usertype'
												onchange="return isClient(this);" style="width: 200px">
												<option value="C">Client</option>
												<?php if($allowed){ ?>
												<option value="S">Staff</option>
												<option value="M">Manager</option>
												<option value="A">Admin</option>
												<?php } ?>
											</select>
											<?php }?>
										</p>
									</td>
									<td><?php if($action =='edit' && isset($user) && $user->usertype == 'C'){?>
										<p id="locationContainer">
											<label>Location<span>(Required Field)</span> </label> <label>
												<input name="type" type="radio" value='D'
												<?php  echo(($user->type !='I') ? 'checked' : '');?>>Domestic
												<input name="type" type="radio" value='I'
												<?php  echo(($user->type =='I') ? 'checked' : '');?>>International
											</label>
										</p> <?php }else if($action == 'add'){ ?>
										<p id="locationContainer">
											<label>Location<span>(Required Field)</span> </label> <label>
												<input name="type" type="radio" value='D' checked>Domestic <input
												name="type" type="radio" value='I'>International </label>
										</p> <?php } ?>
									</td>
								</tr>
								<tr>
									<td>
										<p>
											<label>Username <span>(Required Field)</span> </label> <input
												type="text" name='username' id="username" readonly
												class="field size5" maxlength="100"
												value='<?php echo (!isset($user))?$uname:$user->username; ?>' />
										</p>
									</td>
									<td>
										<p>
											<label>Password <span>(Required Field)</span> </label> <input
												type="text" name='password' id="password" readonly
												class="field size5" maxlength="100"
												value='<?php echo (!isset($user))?'skybest':$user->password; ?>' />
										</p>
									</td>

									<td>
										<p>
											<label>Email <span>(Required Field)</span> </label> <input
												type="text" name='email' id="email@" alt="Email"
												class="field size5" maxlength="100"
												value='<?php echo (!isset($user))?'':$user->email; ?>' />
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<p>
											<label>First Name <span>(Required Field)</span> </label> <input
												type="text" name='firstname' id="firstname*"
												alt="First Name" class="field size5" maxlength="100"
												value='<?php echo (!isset($user))?'':$user->firstname; ?>' />
										</p>
									</td>
									<td>
										<p>
											<label>Last Name <span>(Required Field)</span> </label> <input
												type="text" name='lastname' id="lastname*" alt="Last Name"
												class="field size5" maxlength="100"
												value='<?php echo (!isset($user))?'':$user->lastname; ?>' />
										</p>
									</td>
									<td>
										<p>
											<label>Middle Name </label> <input type="text"
												name='middlename' class="field size5" maxlength="100"
												value='<?php echo (!isset($user))?'':$user->middlename; ?>' />
										</p>
									</td>
								</tr>
								<tr>
									<td colspan="3">
										<p>
											<span class="req"></span> <label>Company Name <span></span> </label>
											<textarea class="field size3" rows="3" style="width: 50%"
												name="companyname">
												<?php echo (!isset($user))?'':$user->companyname; ?>
											</textarea>
										</p>
									</td>
									</td>
								</tr>
							</table>
							<p>
								<span class="req"></span> <label>Address <span></span> </label>
								<textarea class="field size3" rows="3" style="width: 50%"
									name="address">
									<?php echo (!isset($user))?'':$user->address; ?>
								</textarea>
							</p>


							<?php if(false) {//if($action =='edit' && isset($user) && $user->usertype != 'C'){?>
							<table>
								<tr>
									<td style="width: 40px"><input type="checkbox"
										name="emailfornewuser"
										<?php echo (isset($user) && $user->emailfornewuser)?'checked':'' ?>>
									</td>
									<td><label>Email this User when new Client was Added.</label>
									</td>
								</tr>
								<tr>
									<td><input type="checkbox" name="emailfornewtransaction"
									<?php echo (isset($user) && $user->emailfornewtransaction)?'checked':'' ?>>
									</td>
									<td><label>Email this User when new Transacion was made.</label>
									</td>
								</tr>
								<tr>
									<td><input type="checkbox" name="emailforsuccessfuldelivery"
									<?php echo (isset($user) && $user->emailforsuccessfuldelivery)?'checked':''?>>
									</td>
									<td><label>Email this User when new Transacion was successfully
											delivered.</label>
									</td>
								</tr>

							</table>
							<?php }?>

						</div>
						<!-- End Form -->

						<!-- Form Buttons -->
						<div class="buttons">
							<input type="submit" class="button" value="submit" />
						</div>
						<!-- End Form Buttons -->
					</form>



					<!-- Table -->
					<?php
					if(isset($transactionList) && $transactionList->num_rows()>0){
						?>
					<div class="table" style="width: 100%">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<th colspan="8">
									<p>
										<label><br>Client Transactions</label>
									</p>
								</th>
							</tr>
							<tr>

								<th>DR Number</th>
								<th>Commodity</th>
								<th>Pick Up Address</th>
								<th>Delivery<br>Point</th>
								<th>On Board<br>Date</th>
							</tr>

							<?php
							//transactionList
							foreach ($transactionList->result() as $transaction)
							{
								echo "<tr>
											<td>".$transaction->dr_number."</td>
											<td>".$transaction->commodity."</td>
											<td>".$transaction->pickupaddress."</td>
											<td>".$transaction->deliverypoint."</td>
											<td>".$transaction->onboarddate."</td>
											</tr>";

									
							}
							?>

						</table>
					</div>
					<?php
					}
					?>
				</div>

				<!-- End Box -->
				<?php
				}
				?>


			</div>
			<!-- End Content -->

			<!-- Sidebar -->
			<div id="sidebar">


				<!-- End Box -->
			</div>
			<!-- End Sidebar -->

			<div class="cl">&nbsp;</div>
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->




